package longestAlternatingSubarray;

class Solution {
    public int longestAlternatingSubarray(int[] nums, int threshold) {
        int ans = 0;
        int l = 0,r = 0,n = nums.length;
        while (r<n) {
            while (l<n&&(nums[l]%2!=0||nums[l]>threshold)) {
                l++;
            }
            r = l;
            while (r+1<n&&nums[r]%2!=nums[r+1]%2&&nums[r+1]<=threshold) {
                r++;
            }
            if(l<n&&r<n)
                ans = Math.max(ans,r-l+1);
            l = r+1;
        }
        return ans;
    }
}